//
//  MeVCTbv.m
//  ZITHER
//
//  Created by weixin on 2018/10/16.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MeVCTbv.h"
#import "CustomCameraVC.h"
#import "STViewController.h"

#import "MainBizQutoeCellText.h"
#import "MainFootCell.h"
#import "MainHeadCell.h"
#import "MyviewSignaCell.h"
#import "MeVCTbvTypeCell.h"
#import "TaskMeVCCell.h"
#import "Me_MainBtn_Cell_WX.h"

#import "MyEnquiryListNewModel.h"

#import "MeBizQutoeCell.h"
#import "MyEndEnquiryVC.h"
#import "MyInterestVC.h"
#import "Ugc_MyLaud_VC.h"


@interface MeVCTbv()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)MyEnquiryListNewModel * model;//正在询价

@property (nonatomic,strong)NSMutableArray * arrMEndData;//已结束询价

@end


@implementation MeVCTbv

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfiguration];
        [self loadData];
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
            self.backgroundColor = RGB(51, 51, 51);
        }
    }
    return self;
}

-(void)loadData{
    
    if (!kUSER_INFO_MANAGER.userIsLogin) {
        return;
    }
    [[serviceDefualt loadgetMyEnquiryList:[MyEnquiryListNewModel class]]subscribeNext:^(ResponseData *x) {
        if (x.success) {
           self.model = [MyEnquiryListNewModel yy_modelWithJSON:x.responseRawData];
            [self reloadData];
        }else{
            if (![x.errorInfo isEqualToString:@"未登录"]) {
                 [Tools showTips:x.errorInfo];
            }
        }
        [self.mj_header endRefreshing];
        [self reloadData];
    }];
    
    //已结束询价
    [[serviceDefualt loadgetMyEndEnquiryList:0 reponseObjctClass:[MyEnquiryListModel class]]subscribeNext:^(ResponseData *  x) {
        if (x.success) {
            self.arrMEndData = [[NSMutableArray alloc]init];
            NSArray *arr = x.responseRawData[@"data"];
            for (int i = 0; i<arr.count; i++) {
                MyEnquiryListDataModel *model = [MyEnquiryListDataModel yy_modelWithJSON:arr[i]];
                [self.arrMEndData addObject:model];
            }
            [self reloadData];
        }else{
            if (![x.errorInfo isEqualToString:@"未登录"]) {
                 [Tools showTips:x.errorInfo];
            }
        }
    }
     ];

}

-(void)defaultConfiguration{
    self.delegate = self;
    self.dataSource = self;
    self.backgroundColor = HexColor(0xf5f5f5);
}

#pragma mark -- tabdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
//    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
//        return 1;
//    }
    return 6;

//    if (self.arrMEndData.count==0) {
//        return self.model.data.count+2+1+1;
//    }
//        return self.model.data.count+3+1+1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ( indexPath.row == 0 ) {
        return [self getMainHeadCell];
    }else if ( indexPath.row == 1 ) {
        return [self getMeVCTbvTypeCell:indexPath];
    }else{
        return [self getMe_MainBtn_Cell:indexPath];
    }
    
    
    
//    NSInteger index = 0 ;
//    else if ( indexPath.row == index-2 ) {
//        return [self getTaskTypeCell:indexPath];
//    }else if ( indexPath.row == index-1) {
//        return [self getMyviewSignaCell:indexPath];
//    }else if (indexPath.row<=1){
//        return [self getMeVCTbvTypeCell:indexPath];
//    }else{
//        return [self getMe_MainBtn_Cell:indexPath];
//    }

    
    
    
    
    
    
    
    
    
    
    
    
//    if (self.arrMEndData.count==0) {
//        index = self.model.data.count+2+1+1;
//    }else{
//        index = self.model.data.count+3+1+1;
//    }
//
//    if ( indexPath.row == 0 ) {
//        return [self getMainHeadCell];
//    }else if ( indexPath.row == index-2 ) {
//        return [self getTaskTypeCell:indexPath];
//    }else if ( indexPath.row == index-1) {
//        return [self getMyviewSignaCell:indexPath];
//    }else if (indexPath.row<=1){
//        return [self getMeVCTbvTypeCell:indexPath];
//    }else{
//        return [self getMe_MainBtn_Cell:indexPath];
//        return [self getMeBizQutoeCell:indexPath];
//    }
    
}
// 任务cell（a邀请好友）
-(UITableViewCell*)getTaskTypeCell:(NSIndexPath*)indexPath{
    static NSString *sectionCell = @"TaskMeVCCell";
    
    TaskMeVCCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    
    if (cell == nil) {
        cell = [[TaskMeVCCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    return cell;
}
-(UITableViewCell*)getMeVCTbvTypeCell:(NSIndexPath*)indexPath{
    //代码写在这个里面
    static NSString *sectionCell = @"MeVCTbvTypeCell";
    
    MeVCTbvTypeCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    
    if (cell == nil) {
        cell = [[MeVCTbvTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
  
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    [cell setBlockTag:^{
        [self beginUpdates];
        [self endUpdates];
    }];
//    cell.index = indexPath.row-1;
    return cell;
//    @try{
//
//    }
//    @catch(NSException *exception) {
//        NSLog(@"异常错误是:%@", exception);
//    }
//    @finally {
//
//    }
//
}

-(UITableViewCell*)getMyviewSignaCell:(NSIndexPath*)indexPath{
    static NSString *sectionCell = @"MyviewSignaCell";
    
    MyviewSignaCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    
    if (cell == nil) {
        cell = [[MyviewSignaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    [cell UpLoadData];
    return cell;
}
-(UITableViewCell*)getMe_MainBtn_Cell:(NSIndexPath*)indexPath{
    static NSString *sectionCell = @"Me_MainBtn_Cell_WX";
    
    Me_MainBtn_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    
    if (cell == nil) {
        cell = [[Me_MainBtn_Cell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.indexPath = indexPath;
    if (indexPath.row == 1) {
        cell.State = MainBtnTitle_View_Normal;
    }else if (indexPath.row == 5){
        cell.State = MainBtnTitle_View_Pic;

    }else{
        cell.State = MainBtnTitle_View_TitleSub;
    }
    return cell;
}
-(UITableViewCell*)getMeBizQutoeCell:(NSIndexPath*)indexPath{
    static NSString *sectionCell = @"MeBizQutoeCell";
    
    MeBizQutoeCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    
    if (cell == nil) {
        cell = [[MeBizQutoeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    if (indexPath.row == self.model.data.count+1+1 && self.arrMEndData.count!=0) {
        [cell setBizQutoeType:BizQutoeCellREnd];
        cell.indexEnd = self.arrMEndData.count;
        cell.modeTyep1 = self.arrMEndData[0];
    }else{
        [cell setBizQutoeType:BizQutoeCellDef];
        cell.modeTyep1 = self.model.data[indexPath.row-1-1];
    }
    
    return cell;
}

-(UITableViewCell*)getMainHeadCell{
    
    static NSString *sectionCell = @"MainHeadCell";
    
    MainHeadCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    
    if (cell == nil) {
        cell = [[MainHeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    [cell.picUser sd_setImageWithURL:[NSURL URLWithString:self.picUser] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
    
    cell.labUserName.text = self.userName;
    
    cell.labUserPhone.text = kUSER_INFO_MANAGER.phone;
    
    /** 我的消息状态 */
    if (self.NewMsgCont == 0) {
        cell.btnReceive.selected = NO;
        cell.btnReceive.backgroundColor = UIColorWhite;
        [cell.btnReceive setBorderWithColor:[UIColor colorWithHex:@"333333"] width:1];
    }else{
        [cell.btnReceive setTitle:
         [NSString stringWithFormat:@"%ld条新消息",self.NewMsgCont] forState:UIControlStateSelected];
        cell.btnReceive.selected = YES;
        cell.btnReceive.backgroundColor = baseColorTheme;
        [cell.btnReceive setBorderWithColor:baseColorTheme width:1];
    }
    return cell;
}





-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger index = 0 ;
    
    
    if (self.arrMEndData.count==0) {
        index = self.model.data.count+2+1+1;
    }else{
        index = self.model.data.count+3+1+1;
    }
    
    

    
    if ( indexPath.row == 0 ) {

    }else if ( indexPath.row == index-1 || indexPath.row == index-2 ) {

    }else if (indexPath.row<=1){
//        [self cellClick:indexPath.row];
    }else{
        if (indexPath.row == self.model.data.count+1+1 && self.arrMEndData.count!=0) {
            [self.getNav pushViewController:[[MyEndEnquiryVC alloc]init] animated:YES];
        }else{
            STViewController *vc = [[STViewController alloc]init];
            vc.type = STControllerTypeHiddenNavBar;
            vc.goodId = self.model.data[indexPath.row-1-1].userEnquiryInfo.goodsId;
            [self.getNav pushViewController:vc animated:YES];
        }
        
    }

}



-(NSMutableArray *)arrMEndData{
    if (!_arrMEndData) {
        _arrMEndData = [[NSMutableArray alloc]init];
    }
    return _arrMEndData;
}
@end
